<?php
namespace Home\Controller;
use \Think\Controller;
header("content-type:text/html;charset=utf-8");
class SpaceController extends Controller
{
	public function __construct()
	{
		parent::__construct();
		if (empty($_SESSION['uid']) && empty($_GET['uid'])) {
			$this->display('User/login');
		}
	}
	public function index()
	{
		$uid=$_GET['uid'];
		$username=M('user')->where('uid='.$uid)->getField('username');
		$this->assign('username',$username);
		$user=M('user');
		$where['username']=$username;
		$data=$user->where($where)->getField('userimage');
		$score=$user->where($where)->getField('score');
		if ($data==null) {
			$path='noavatar_middle.gif';
		}else{
			$path='120_'.$data;
		}
		$this->assign('image',$path);
		$this->assign('score',$score);
		$uname=$_SESSION['username'];
		$this->assign('uname',$uname);

		//首页相册模块
		$album=M('user_album')->where('uid='.$uid)->select();
		$this->assign('album',$album);
		//判断是否有相册
		if($album)
		{
			$this->assign('album',1);
		}else{
			$this->assign('album',0);
		}
		//查询相册的封面照片
		$F_image=M()->table(array("bbs_user_image"=>"ui","bbs_user_album"=>"ua"))->field("ui.alid,ui.iname,ua.alname")->where("ui.uid=ua.uid AND ui.uid=$uid AND ui.alid=ua.alid AND ui.is_face=1")->select();
		// dump($F_image);
		$this->assign('F_image',$F_image);

		//查询个人资料
		$ziliao=M('user_ziliao')->where('uid='.$uid)->select();
		// dump($ziliao);
		$this->assign('ziliao',$ziliao);
		$pro=M('address')->where('id='.intval($ziliao[0]['pro']))->select();
		// dump($pro);
		$this->assign('pro',$pro);
		$city=M('address')->where('id='.intval($ziliao[0]['city']))->select();
		$this->assign('city',$city);
		$area=M('address')->where('id='.intval($ziliao[0]['area']))->select();
		$this->assign('area',$area);
		$street=M('address')->where('id='.intval($ziliao[0]['street']))->select();
		$this->assign('street',$street);

		//查询友情链接
		$youqing=M('youqing')->select();
		$this->assign('youqing',$youqing);
		$this->display();
	}

	public function dongtai()
	{
		$uid=$_GET['uid'];
		$username=M('user')->where('uid='.$uid)->getField('username');
		$this->assign('username',$username);
		$this->assign('uid',$uid);
		$uname=$_SESSION['username'];
		$this->assign('uname',$uname);

		$user=M('user');
		$where['username']=$username;
		$data=$user->where($where)->getField('userimage');
		if ($data==null) {
			$path='noavatar_middle.gif';
		}else{
			$path='120_'.$data;
		}
		$this->assign('image',$path);
		$youqing=M('youqing')->select();
		$this->assign('youqing',$youqing);
		$this->display();
	}

	public function edithead()
	{
		$uid=$_GET['uid'];
		$username=M('user')->where('uid='.$uid)->getField('username');
		$this->assign('username',$username);
		//获取头像路径
		$user=M('user');
		$where['username']=$username;
		$data=$user->where($where)->getField('userimage');
		
		//dump($data);
		if ($data==null) {
			$path='noavatar_middle.gif';
		}else{
			$path='150_'.$data;
		}
		$this->assign('image',$path);
		$youqing=M('youqing')->select();
		$this->assign('youqing',$youqing);
		$this->display();
	}
	public function upload()
	{
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath = './Public/'; // 设置附件上传根目录
		$upload->savePath = 'Home/image/space/touxiang/'; // 设置附件上传（子）目录
		$upload->saveName='time';//设置上传文件名
		$upload->autoSub=false;
		// 上传文件
		$info = $upload->upload();
		if(!$info) {// 上传错误提示错误信息
		$this->error($upload->getError());
		}else{// 上传成功 取得上传文件的信息
			
			foreach ($info as $v) 
			{
				$model=M('user');
				$data['userimage'] = $v['savename'];
				$model->create($data);
				$where['uid']=$_GET['uid'];
				$list=$model->where($where)->save($data);
				
				if ($list!==false) {
					//生成缩略图
					$image=new \Think\Image();
					$image->open('./Public/'.$v['savepath'].$v['savename']);
					$image->thumb(150,150)->save('./Public/'.$v['savepath'].'150_'.$v['savename']);
					$image->thumb(120,120)->save('./Public/'.$v['savepath'].'120_'.$v['savename']);
					

					$this->redirect('Space/edithead',array('uid'=>$_SESSION['uid']));
				}else{
					$this->error('上传图片失败！');
				}
			}
		}	
	}
	public function ziliao()
	{
		$uid=$_GET['uid'];
		$username=M('user')->where('uid='.$uid)->getField('username');
		$this->assign('username',$username);
		//接收参数
		$uid=I('get.uid/d');
		//dump($uid);
		$data=M('user_ziliao')->where('uid='.$uid)->find();
		// dump($data);
		$this->assign('data',$data);

		$youqing=M('youqing')->select();
		$this->assign('youqing',$youqing);
		$this->display();
	}

	//执行编辑
	public function ziliaoedit()
	{
		//dump($_POST);die;
		 //判断 有无传递POST
        if (empty($_POST)) {
            $this->error('请完善数据!');
            exit;
        }
        M('user_ziliao')->create();
      // var_dump($result);die;
        if (M('user_ziliao')->where('uid='.$_SESSION['uid'])->save() > 0) {
            $this->redirect('Space/ziliao',array('uid'=>$_POST['uid']));
        } else {
        	//M('user_ziliao')->create()->add();
            if(M('user_ziliao')->data($_POST)->add()>0){
            	$this->redirect('Space/ziliao',array('uid'=>$_POST['uid']));
            }else{
            	$this->error('编辑失败');
            }
        }
	}
	public function address()
	{
		$model=M('address');
		//接收查询条件
		$upid=empty($_GET['upid'])?0:$_GET['upid'];
		$data=$model->where('upid='.$upid)->select();
		echo json_encode($data);
	}

	public function album()
	{
		$uid=$_GET['uid'];
		$username=M('user')->where('uid='.$uid)->getField('username');
		$this->assign('username',$username);
		$album=M('user_album')->where('uid='.$_GET['uid'])->select();
		// dump($album);die;
		//判断是否有相册
		if($album)
		{
			$this->assign('album',1);
		}else{
			$this->assign('album',0);
		}
		//遍历相册的数据
		$data=M()->table(array("bbs_user_image"=>"ui","bbs_user_album"=>"ua"))->field("ui.alid,ui.iname,ui.itime,ua.alname,ua.altext")->where("ui.uid=ua.uid AND ui.uid={$uid} AND ui.alid=ua.alid AND ui.is_face='1'")->select();
		$this->assign('data',$data);
		$uname=$_SESSION['username'];
		$this->assign('uname',$uname);

		$user=M('user');
		$where['username']=$username;
		$data=$user->where($where)->getField('userimage');
		if ($data==null) {
			$path='noavatar_middle.gif';
		}else{
			$path='120_'.$data;
		}
		$this->assign('image',$path);

		$youqing=M('youqing')->select();
		$this->assign('youqing',$youqing);

		$this->display();	
	}

	public function imageUpload()
	{
		$uid=$_GET['uid'];
		$username=M('user')->where('uid='.$uid)->getField('username');
		$this->assign('username',$username);
		$uname=$_SESSION['username'];
		$this->assign('uname',$uname);

		$youqing=M('youqing')->select();
		$this->assign('youqing',$youqing);
		$this->display();	
	}

	public function doUpload()
	{
		//创建相册
		if(empty($_POST))
		{
			$this->error('相册创建失败！');
		}else{
			$data['alname']=$_POST['alname'];
			$data['altext']=$_POST['altext'];
			$data['uid']=I('get.uid/d');
			//dump($data);die;
			M('user_album')->data($data)->add();
			$alid=M('user_album')->where($data)->getField('alid');
			$alid=intval($alid);
			//dump($alid);die;
			//在相册中上传图片
			//上传照片
		    $upload = new \Think\Upload();// 实例化上传类
		    $upload->maxSize = 3145728 ;// 设置附件上传大小
		    $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		    $upload->rootPath = './Public/'; // 设置附件上传根目录
		    $upload->savePath = 'Home/image/space/album/'; // 设置附件上传（子）目录
		    $upload->saveName='time';//设置上传文件名
		    $upload->autoSub=false;
		    // 上传文件
		    $info = $upload->upload();
		    if(!$info) {// 上传错误提示错误信息
			    $this->error($upload->getError());
		    }else{// 上传成功 取得上传文件的信息
			
			    foreach ($info as $v) 
			    {
				    $model=M('user_image');
				    $data1['iname'] = $v['savename'];
				    $data1['itime'] = date('Y-m-d H:i:s');
				    $data1['uid']=I('get.uid/d');
				    $data1['alid']=$alid;
				    $data1['is_face']= 1;
				    $list=$model->add($data1);
				    if ($list!==false) {
					    //生成缩略图
					    $image=new \Think\Image();
					    $image->open('./Public/'.$v['savepath'].$v['savename']);
					    $image->thumb(150,150)->save('./Public/'.$v['savepath'].'150_'.$v['savename']);
					    $image->thumb(120,120)->save('./Public/'.$v['savepath'].'120_'.$v['savename']);
					    $this->redirect('Space/album',array('uid'=>$_SESSION['uid']));
				    }else{
					    $this->error('上传图片失败');
				    }
			    }
		    }
		}
	}

	//打开相册后的图片列表
	public function imglist()
	{
		$uid=$_GET['uid'];
		$username=M('user')->where('uid='.$uid)->getField('username');
		$this->assign('username',$username);
		$imgs=M('user_image')->where('alid='.$_GET['alid'])->select();
		//dump($imgs);
		$this->assign('imgs',$imgs);
		// dump($_SESSION);

		$user=M('user');
		$where['username']=$username;
		$data=$user->where($where)->getField('userimage');
		if ($data==null) {
			$path='noavatar_middle.gif';
		}else{
			$path='120_'.$data;
		}
		$this->assign('image',$path);

		$youqing=M('youqing')->select();
		$this->assign('youqing',$youqing);
		$this->display();
	}
	//执行相册删除
	public function delalbum()
	{
		$alid=I('get.alid/d');
		if(M('user_image')->where('alid='.$alid)->delete()>0)
		{
			M('user_album')->delete($alid);
			$this->redirect('Space/album',array('uid'=>$_SESSION['uid']));
		}else{
			$this->error('Space/album',array('uid'=>$_SESSION['uid']));
		}
	}

	public function editalbum()
	{
		$uid=$_GET['uid'];
		$username=M('user')->where('uid='.$uid)->getField('username');
		$this->assign('username',$username);
		$album=M('user_album')->where('alid='.$_GET['alid'])->select();
		// dump($album);
		$this->assign('album',$album);
		$youqing=M('youqing')->select();
		$this->assign('youqing',$youqing);
		$this->display();
	}
	//执行相册修改
	public function doEditalbum()
	{
		// dump($_POST);die;
		if (empty($_POST)) {
            $this->error('请完善数据!');
            exit;
        }
        M('user_album')->create();
      // var_dump($result);die;
        if (M('user_album')->where('alid='.$_POST['alid'])->save() > 0) {
            $this->redirect('Space/album',array('uid'=>$_SESSION['uid']));
        } else {
       
            $this->error('编辑失败');
        }
	}
	public function uploadimg()
	{
		$uid=$_GET['uid'];
		$username=M('user')->where('uid='.$uid)->getField('username');
		$this->assign('username',$username);

		$youqing=M('youqing')->select();
		$this->assign('youqing',$youqing);
		$this->display();
	}
	//执行照片上传
	public function doUploadimg()
	{
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath = './Public/'; // 设置附件上传根目录
		$upload->savePath = 'Home/image/space/album/'; // 设置附件上传（子）目录
		$upload->saveName='time';//设置上传文件名
		$upload->autoSub=false;
		// 上传文件
		$info = $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
		}else{// 上传成功 取得上传文件的信息
			
			foreach ($info as $v) 
			{
				$model=M('user_image');
				$data1['iname'] = $v['savename'];
				$data1['itime'] = date('Y-m-d H:i:s');
				$data1['uid']=I('get.uid/d');
				$data1['alid']=$_GET['alid'];
				$data1['is_face']= 0;
				$list=$model->add($data1);
				if ($list!==false) {
					//生成缩略图
					$image=new \Think\Image();
					$image->open('./Public/'.$v['savepath'].$v['savename']);
					$image->thumb(150,150)->save('./Public/'.$v['savepath'].'150_'.$v['savename']);
					$image->thumb(120,120)->save('./Public/'.$v['savepath'].'120_'.$v['savename']);
					$this->redirect('Space/imglist',array('uid'=>$_GET['uid'],'alid'=>$_GET['alid']));
				}else{
					$this->error('上传图片失败');
				}
			}
		}
	}

	public function deleteimg()
	{
		$alid=I('get.alid/d');
		$iid=I('get.iid/d');
		$img=M('user_image')->where('iid='.$iid)->getfield('is_face');
		//dump($img);die;
		if($img!=1)
		{
			M('user_image')->where('iid='.$iid)->delete();
			$this->redirect('Space/imglist',array('uid'=>$_SESSION['uid'],'alid'=>$alid));
		}else{
			$this->error('无法删除相册封面！');
		}	
	}

	public function setFace()
	{
		$iid=I('get.iid/d');
		$alid=I('get.alid/d');
		$data['is_face']=0;
		if(M('user_image')->where('alid='.$alid)->save($data)>0)
		{
			$data1['is_face']=1;
			M('user_image')->where('iid='.$iid)->save($data1);
			$this->redirect('Space/imglist',array('uid'=>$_SESSION['uid'],'alid'=>$alid));
		}else{
			$this->error('修改失败！');
		}
	}

	public function liuyan()
	{
		$lyuid=I('session.uid/d');
		$blyuid=I('get.uid/d');
		$lytime=date('Y-m-d H:i:s');
		$this->assign('lyuid',$lyuid);
		$this->assign('blyuid',$blyuid);
		$this->assign('lytime',$lytime);
		$uname=M('user')->where('uid='.$lyuid)->getField('username');
		$this->assign('uname',$uname);
		$data=$this->get_str($_GET['uid']);
		//dump($data);
		$this->assign('data',$data);

		$uid=$_GET['uid'];
		$username=M('user')->where('uid='.$uid)->getField('username');
		$this->assign('username',$username);
		$user=M('user');
		$where['username']=$uname;
		$data=$user->where($where)->getField('userimage');
		if ($data==null) {
			$path='noavatar_middle.gif';
		}else{
			$path='120_'.$data;
		}
		$this->assign('image',$path);
		$youqing=M('youqing')->select();
		$this->assign('youqing',$youqing);
		$this->display();
	}
	public function doLiuyan()
	{
		$data['blyuid']=$_POST['blyuid'];
		$data['lytime']=$_POST['lytime'];
		$data['lyuid']=I('post.lyuid/d');
		$data['lytext']=$_POST['lytext'];
		$data['pid']=I('pid',0);
		$liuyan=M('liuyan')->data($data)->add();
		$data['lyid'] = $liuyan;
		$data['uname']=$_POST['uname'];
		echo json_encode($data);
		die;
	}

	private function get_str(&$blyuid=0,$pid = 0,&$arr = array()) 
	{ 	
		$liuyan=M('liuyan as ly')->join('bbs_user as u on u.uid=ly.lyuid')->where('ly.pid='.$pid.' And ly.blyuid='.$blyuid)->select();
		//var_dump($liuyan);
		foreach($liuyan as $key =>$value){
			if($value['pid'] !=0)
			{
				$value['username']='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$value['username'];
				$value['lytext']='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$value['lytext'];
				$value['lytime']=$value['lytime'];
			}
			$arr[] = $value;
			$this->get_str($blyuid,$value['lyid'],$arr);
		}
	    return $arr; 
	} 

	// 收藏管理
	public function fav()
	{
		$uid=$_GET['uid'];
		$username=M('user')->where('uid='.$uid)->getField('username');
		$this->assign('username',$username);
		$user=M('user');
		$where['username']=$username; 
		$data=$user->where($where)->getField('userimage');
		if ($data==null) {
			$path='noavatar_middle.gif';
		}else{
			$path='120_'.$data;
		}

		// 查出收藏的帖子
		$fav = M('favorite');
		
		$where['uid'] = $uid;
		$favdata = $fav->where($where)->select();
		if (empty($favdata)) {
			$this->assign('tag',0);
		} else {
			$this->assign('tag',1);
			$this->assign('data',$favdata);
		}
		$this->assign('image',$path);
		$this->display('Space/fav');
	}

	// 删除收藏
	public function delfav()
	{
		$fav = M('favorite');
		$where['fid'] = $_GET['fid'];
		$result = $fav->where($where)->delete();
		if ($result >= 1) {
			$this->assign('data',$favdata);
			$this->success('删除成功!');
		} else {
			$this->error('删除失败...');
		}
		
	}

	// 个人资料
	public function personal()
	{
		$uid=$_GET['uid'];
		$username=M('user')->where('uid='.$uid)->getField('username');
		$this->assign('username',$username);
		$user=M('user');
		$where['username']=$username;
		$data=$user->where($where)->getField('userimage');
		if ($data==null) {
			$path='noavatar_middle.gif';
		}else{
			$path='120_'.$data;
		}
		$this->assign('image',$path);
		$uname=$_SESSION['username'];
		$this->assign('uname',$uname);

		//查询个人资料
		$ziliao=M('user_ziliao')->where('uid='.$uid)->select();
		// dump($ziliao);
		$this->assign('ziliao',$ziliao);
		$pro=M('address')->where('id='.intval($ziliao[0]['pro']))->select();
		// dump($pro);
		$this->assign('pro',$pro);
		$city=M('address')->where('id='.intval($ziliao[0]['city']))->select();
		$this->assign('city',$city);
		$area=M('address')->where('id='.intval($ziliao[0]['area']))->select();
		$this->assign('area',$area);
		$street=M('address')->where('id='.intval($ziliao[0]['street']))->select();
		$this->assign('street',$street);

		$youqing=M('youqing')->select();
		$this->assign('youqing',$youqing);
		$this->display();
	}
}